package com.ruoyi.email;

import com.ruoyi.RuoYiApplication;
import com.ruoyi.WarnNotification.service.IWarningNotificationService;
import com.ruoyi.user.service.IAppUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.GetMapping;


/**
 * eamil测试类
 *
 * @author rwd
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class emailTest {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private IWarningNotificationService warningNotificationService;
    @Autowired
    private IAppUserService IAppUserService;

@GetMapping("/sendEmail")
public void send(long currentUserId){
    try {
//        Long currentUserId = Long.valueOf(111);
        // 修改为实时获取当前用户ID
//        Long currentUserId = SecurityUtils.getLoginUser().getUserId();

        // 获取通知数据（需实现NotificationService）
        String notification = warningNotificationService.getUsernotifyContent(currentUserId);

        // 根据userID获取邮箱
        String userEmail = IAppUserService.getEmailByUserId(currentUserId);

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("2920957718@qq.com");
        //邮件主题
        mailMessage.setSubject("康悦健康平台异常通知");
        mailMessage.setText(notification); // 动态内容
        mailMessage.setTo(userEmail); // 动态收件人

        javaMailSender.send(mailMessage);
        System.out.println("成功发送至：" + userEmail);
    } catch (MailException e) {
        System.err.println("邮件发送失败: " + e.getMessage());
    } catch (Exception e) {
        System.err.println("系统异常: " + e.getMessage());
        e.printStackTrace();
    }
}

}
